<template>
  <div class="tabls">
    <div>
      <el-input v-model="searchForm.keyword" style="width: 240px;margin-right: 10px;" placeholder="请输入" />
      <el-button type="primary" @click="handleSearch">查询</el-button>
    </div>
    <el-table :data="tableData" stripe style="width: 100%" height="525" border>
      <el-table-column prop="id" label="编号" />
      <el-table-column prop="name" label="品牌名称" />
      <el-table-column prop="firstLetter" label="品牌首字母" />
      <el-table-column prop="sort" label="排序" />
      <el-table-column prop="sort" label="品牌制造商">
        <template #default="scope">
          <el-switch v-model="scope.row.factoryStatus" :active-value="1" :inactive-value="0" />
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="相关" width="200">
        <template #default="scope">
          <span>商品：100</span>
          <span>评价：1000</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageBox">
      <el-pagination v-model:current-page="searchForm.pageNum" v-model:page-size="searchForm.pageSize"
        :page-sizes="[10, 20, 30, 40]" layout="total, prev, pager, next,sizes" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>

  </div>


</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { nanoid } from 'nanoid'
import { ElMessage } from 'element-plus'
import { fetchList } from '@/api/brand.js'

let tableData = ref([])
let total = ref(0)
let searchForm = reactive({
  pageNum: 1,
  pageSize: 10,
  keyword: ''
})

onMounted(() => {
  getBrandList()
})

const handleSearch = () => {
  getBrandList()
}
const handleSizeChange = (num: any) => {
  searchForm.pageSize = num
  getBrandList()
}

const handleCurrentChange = (num: any) => {
  searchForm.pageNum = num
  getBrandList()
}

//获取品牌列表
const getBrandList = () => {
  fetchList(searchForm).then(res => {
    if (res.code == 200) {
      tableData.value = res.data.list
      total.value = res.data.total
      console.log(tableData)
    }
  }).catch(err => { })
}

function handleEdit(index: number, row: object) {
  console.log(index, row)
  // ElMessage({
  //   message: '编辑成功',
  //   type: 'success',
  // })
}

function handleDelete(index: number, row: object) {
  console.log(index, row)
  // ElMessage('删除成功')
}

</script>

<style scoped>
.tabls {
  padding: 10px;
}

img {
  width: 100px;
}

.pageBox {
  display: flex;
  justify-content: end;
  margin-top: 10px;
}
</style>